package com.runa.rccprotocol.analysis.daiao;

import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.persistence.DateUtil;
import com.runa.protocol.dto.IInterData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 2024/12/20 10:34
 * zhangYB
 */
@Data
@Slf4j
public class DaiAoAnalysis {

    private IInterData interData;


    private float toKw(String s, int vif) {
        Float v = Float.valueOf(s);
        switch (vif) {
            case 0x2A:
                v = v * 0.1f * 0.001f;
                break;
            case 0x2B:
                v = v * 0.001f;
                break;
            case 0x2C:
                v = v * 10f * 0.001f;
                break;
        }
        return v;
    }

    private float toKwh(String s, int vif) {
        Float v = Float.valueOf(s);
        switch (vif) {
            case 0x05:
                v = v * 0.1f;
                break;
            case 0x07:
                v = v * 10f;
                break;
            case 0x0F:
                v = v * 10f * 1000f / 3.6f;
                break;
            case 0x0E:
                v = v * 1000f / 3.6f;
                break;
            case 0x0D:
                v = v * 0.1f * 1000f / 3.6f;
                break;
        }
        return v;
    }


    private float toM3(String s, int vif) {
        Float v = Float.valueOf(s);
        switch (vif) {
            case 0x12:
                v = v * 0.1f * 0.001f;
                break;
            case 0x13:
                v = v * 0.001f;
                break;
            case 0x14:
                v = v * 10f * 0.001f;
                break;
        }
        return v;
    }

    private float toM3h(String s, int vif) {
        Float v = Float.valueOf(s);
        switch (vif) {
            case 0x3A:
                v = v * 0.1f * 0.001f;
                break;
            case 0x3B:
                v = v * 0.001f;
                break;
            case 0x3C:
                v = v * 10f * 0.001f;
                break;
        }
        return v;
    }

    public int handleMeter(byte[] data, int begin, int length) {
        HotMeterVO hotMeterVO = new HotMeterVO();
        hotMeterVO.setMeterSerial(ByteUtil.byteToHexStringFlip(data, begin+7, 4));
        hotMeterVO.setReadDate(new Date());
        try {
            for (int i = 0; i < length; i++) {
                if ((data[i] & 0xff) == 0x0C) {
                    int vif1 = data[i + 1] & 0xff;
                    if (vif1 == 0xFB) {
                        if ((data[i + 2] & 0xff) == 0x81) {
                            Float v = Float.valueOf(ByteUtil.byteToHexStringFlip(data, i + 4, 4));
                            if ((data[i + 3] & 0xff) == 0x72) {
                                v = v * 0.1f;
                                hotMeterVO.setAccHeat(v);
                                i += 7;
                            } else if ((data[i + 3] & 0xff) == 0x73) {
                                hotMeterVO.setAccHeat(v);
                                i += 7;
                            } else if ((data[i + 3] & 0xff) == 0x74) {
                                v = v * 10f;
                                hotMeterVO.setAccHeat(v);
                                i += 7;
                            }
                        }
                    } else if (vif1 == 0x86 && (data[i + 2] & 0xff) == 0x75) {
                        String s = ByteUtil.byteToHexStringFlip(data, i + 3, 4);
                        Float v = Float.valueOf(s) * 0.1f;
                        hotMeterVO.setAccHeat(v);
                        i += 6;
                    } else {
                        String s = ByteUtil.byteToHexStringFlip(data, i + 2, 4);
                        switch (vif1) {
                            case 0x05:
                            case 0x06:
                            case 0x07:
                            case 0x0F:
                            case 0x0E:
                            case 0x0D:
                                hotMeterVO.setAccHeat(toKwh(s, vif1));
                                i += 5;
                                break;
                            case 0x2A:
                            case 0x2B:
                            case 0x2C:
                                hotMeterVO.setPower(toKw(s, vif1));
                                i += 5;
                                break;
                            case 0x12:
                            case 0x13:
                            case 0x14:
                                hotMeterVO.setAccFlow(toM3(s, vif1));
                                i += 5;
                                break;
                        }
                    }

                } else if ((data[i] & 0xff) == 0x0B) {
                    int vif1 = data[i + 1] & 0xff;
                    String s = ByteUtil.byteToHexStringFlip(data, i + 2, 3);
                    switch (vif1) {
                        case 0x3A:
                        case 0x3B:
                        case 0x3C:
                            hotMeterVO.setFlow(toM3h(s, vif1));
                            i += 4;
                    }
                } else if ((data[i] & 0xff) == 0x04) {
                    int vif1 = data[i + 1] & 0xff;
                    if (vif1 == 0x6D) {
                        hotMeterVO.setMeterDate(toDate(data, vif1, i+2));
                        i += 5;
                    }
                } else if ((data[i] & 0xff) == 0x0A && (data[i+1] & 0xff) == 0x5A) {
                    hotMeterVO.setSupplyTemp(Integer.valueOf(ByteUtil.byteToHexStringFlip(data, i+2, 2)) * 0.1f);
                    i += 3;
                } else if ((data[i] & 0xff) == 0x0A && (data[i+1] & 0xff) == 0x5E) {
                    hotMeterVO.setReturnTemp(Integer.valueOf(ByteUtil.byteToHexStringFlip(data, i+2, 2)) * 0.1f);
                    i += 3;
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }

        RunaConcenResponseData runaConcenResponseData = new RunaConcenResponseData();
        runaConcenResponseData.addRepData(hotMeterVO);
        this.interData = runaConcenResponseData;
        return 0;

    }

    private Date toDate(byte[] data, int vif1, int i) {
        if (vif1 == 0x6D) {
            byte b1 = data[i++];
            byte b2 = data[i++];
            byte b3 = data[i++];
            byte b4 = data[i++];
            String minutes = String.valueOf(b1 & 0b111111);
            String hours = String.valueOf(b2 & 0b11111);
            String days = String.valueOf(b3 & 0b11111);
            String mouths = String.valueOf(b4 & 0b1111);
            String years = String.valueOf(((b4 >> 4 & 0b1111) << 3) | (b3 >> 5 & 0b111));
            String s = years + mouths + days + hours + minutes;
            return DateUtil.stringToDate(s, "yyMMddHHmm");
        }
        return null;
    }


    public static void main(String[] args) {
        String s = "68A4A4680800727085284224232E0CF43000000C05965808008C1005000000008C2005000000000C12556736100C2A963307000B3A0682000A5A77050A5E99040A6277000B26008610046D0E0C143C4C05394600004C1218128800CC100500000000CC200500000000426C013642EC7E21368C0105690100008C0112049800008C1105000000008C21050000000082016CFF2C8201EC7E1F3C8C4014000000008C80401400000000FB16";
        byte[] data = ByteUtil.hexStringToByte(s);
        DaiAoAnalysis daiAoAnalysis = new DaiAoAnalysis();
        daiAoAnalysis.handleMeter(data, 0, data.length);

    }




}
